package actions;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import javax.enterprise.inject.New;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

import db.dao.CourClass;
import db.dao.CourClassDAO;
import db.dao.Course;
import db.dao.CourseDAO;
import db.dao.Teacher;
import db.dao.TeacherDAO;
import db.dao.Users;
import db.dao.UsersDAO;

/*查找开课班级信息*/
public class SearchCourClassAction extends ActionSupport{
	private String courclassOption="";	//查找选项
	private String property="";	//值

	public SearchCourClassAction() {
	}

	public String getCourclassOption() {
		return courclassOption;
	}

	public void setCourclassOption(String courclassOption) {
		this.courclassOption = courclassOption;
	}

	public String getProperty() {
		return property;
	}

	public void setProperty(String property) {
		this.property = property;
	}

	public String execute() throws IOException {
		//获取页面信息，用于弹窗提示和清除页面缓存
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		//用于传递参数，判断查询内容
		HttpSession session = request.getSession();
		//根据用户选择的查询条件和输入的值查找开课班级信息
		CourClassDAO ccDao = new CourClassDAO();
		List<CourClass> list = new ArrayList<CourClass>();
		Set<CourClass> sCourClasses = new HashSet<CourClass>();
		Iterator<CourClass> iterator = sCourClasses.iterator();
		if (courclassOption.equals("courseid")) {
			list = ccDao.findByProperty("id.courseid", property);
		}else if (courclassOption.equals("teacherid")) {
			list = ccDao.findByProperty("id.teaid", property);
		}else if (courclassOption.equals("coursename")) {
			CourseDAO cDao = new CourseDAO();
			List<Course> cList = new ArrayList<Course>();
			cList = cDao.findByPropertyLike("coursename", property+"%");
			Iterator<Course> ic = cList.iterator();
			while (ic.hasNext()) {
				sCourClasses = ic.next().getCourClasses();
				iterator = sCourClasses.iterator();
				while (iterator.hasNext()) {
					list.add(iterator.next());
				}
			}
			
		}else if (courclassOption.equals("teachername")) {
			TeacherDAO tDao = new TeacherDAO();
			List<Teacher> tList = new ArrayList<Teacher>();
			tList = tDao.findByPropertyLike("teaname", property+"%");
			Iterator<Teacher> it = tList.iterator();
			while (it.hasNext()) {
				sCourClasses = it.next().getCourClasses();
				iterator = sCourClasses.iterator();
				while (iterator.hasNext()) {
					list.add(iterator.next());
				}
			}
		}else if (courclassOption.equals("all")) {
			list = ccDao.findByProperty("id.teaid", session.getAttribute("username"));
		}
		if (!list.isEmpty()) {
			//成功传递信息
			session.setAttribute("courclasses", list);
			session.setAttribute("search", new String("courclasses"));
			response.setHeader("Pragma","No-cache");   
			response.setHeader("Cache-Control","no-cache");  
			response.setHeader("Cache-Control", "no-store");  
			response.setDateHeader("Expires", 0);
			return "success";
		}else {
			//失败弹窗提示
			response.setContentType("text/html;charset=UTF-8");
            response.setCharacterEncoding("UTF-8");
            PrintWriter printWriter = response.getWriter();
            printWriter.print("<script>alert('未查询到相关信息!')</script>");
            UsersDAO uDao = new UsersDAO();
            Users users = uDao.findById((String)session.getAttribute("username"));
            if (users.getUsertype().equals("管理")) {
				printWriter.print("<script>window.location.href='/GroupProject/manage.jsp'</script>");
			}else if (users.getUsertype().equals("教师")) {
				printWriter.print("<script>window.location.href='/GroupProject/teacher.jsp'</script>");
			}else {
				printWriter.print("<script>window.location.href='/GroupProject/student.jsp'</script>");
			}
            printWriter.flush();
            printWriter.close();
            response.setHeader("Pragma","No-cache");   
            response.setHeader("Cache-Control","no-cache");  
            response.setHeader("Cache-Control", "no-store");  
            response.setDateHeader("Expires", 0);
			return null;
		}
	}
}
